package com.sheep.jucdemo.part11.part11_4;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @Date 2022/5/24 23:44
 * @Created by sheep
 */
public class Test11_4_1_3 {
    // 创建ThreadLocal实例
    static ThreadLocal<DateFormat> safeSdf = new ThreadLocal<DateFormat>(){
        @Override
        protected SimpleDateFormat initialValue(){
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };
    public static void main(String[] args) {
        // 创建多个线程启动
        for (int i = 0; i < 10; ++i) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(safeSdf.get().parse("2022-05-24 23:35:27"));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }finally {
                        // 使用完毕一定要清除，避免内存泄漏
                        safeSdf.remove();
                    }
                }
            });
            // 启动线程
            thread.start();
        }
    }
}
